{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "private_outputs": true,
      "provenance": [],
      "authorship_tag": "ABX9TyMgf8iiblWS1VyZDntkxd2Y",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/ktynski/Marketing_Automations_Notebooks_With_GPT/blob/main/Prompt_Chaining_Press_Earning_Data_Journalism_Stories_(Public).ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "\n",
        "http://www.Frac.tl - My Agency, we earn press and massively improve the ranking potential of our client's domains through a data-journalism style approach to earned media. We are excited about the limitless possibilities of AI enabled processes in Content Marketing, SEO, and PR. Email me at Kristin@frac.tl if you would like to learn more about our services."
      ],
      "metadata": {
        "id": "NzUFt8KpmYMV"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install openai\n",
        "!pip install langchain"
      ],
      "metadata": {
        "id": "6lVMdHPnR4Pl"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "import openai\n",
        "import pandas as pd\n",
        "import re\n",
        "\n",
        "# Set up OpenAI API key\n",
        "openai.api_key = \"your OpenAI API Key\"\n",
        "\n",
        "\n",
        "# Set up the topic\n",
        "topic = \"Masters Degrees\"\n",
        "\n",
        "# Use openai to generate a list of subtopics for the given topic\n",
        "def generate_subtopics():\n",
        "    response = openai.Completion.create(\n",
        "        engine=\"text-davinci-003\",\n",
        "        prompt=f\"You are a NYTimes Upshot data-journalist. You are working on new story ideas. Please begin by suggesting a list of 5 newsworthy subtopics that fall under the main topic of: {topic}. Ideas: \\n 1.\",\n",
        "        max_tokens=100,\n",
        "        n=1,\n",
        "        stop=None,\n",
        "        temperature=0.5\n",
        "    )\n",
        "    return response.choices[0].text.strip()\n",
        "\n",
        "subtopics = re.findall(r\"\\d+\\. (.*)\", generate_subtopics())\n",
        "\n",
        "# Use gpt3 to provide a list of 10 example datasources for each subtopic\n",
        "def generate_datasources(subtopic):\n",
        "    response = openai.Completion.create(\n",
        "        engine=\"text-davinci-003\",\n",
        "        prompt=f\"Provide a numbered List of 5 relevant data sources for the subtopic, draw on ideas from proprietary data sources, scrapable sources, government data sources, or social media data sources that might be relevant to telling stories about the subtopic.\\n\\nSubtopic: {subtopic}. Ideas: \\n 1.\",\n",
        "        max_tokens=200,\n",
        "        n=1,\n",
        "        stop=None,\n",
        "        temperature=0.5\n",
        "    )\n",
        "    return response.choices[0].text.strip()\n",
        "\n",
        "datasources = {}\n",
        "for subtopic in subtopics:\n",
        "    datasources[subtopic] = re.findall(r\"\\d+\\. (.*)\", generate_datasources(subtopic))\n",
        "\n",
        "# Use gpt3 to generate a list of story ideas for each subtopic/datasource combination\n",
        "def generate_story_ideas(subtopic, datasource):\n",
        "    response = openai.Completion.create(\n",
        "        engine=\"text-davinci-003\",\n",
        "        prompt=f\"Come up with a numbered list of 3  data-journalism story ideas that tell an important and newsworthy story that is also emotionally evocative. Describe how the data source would be used to tell the story. Use the {datasource} to tell a story about {subtopic}. Ideas: \\n 1.\",\n",
        "        max_tokens=700,\n",
        "        n=1,\n",
        "        stop=None,\n",
        "        temperature=0.9\n",
        "    )\n",
        "    examples = response.choices[0].text.strip()\n",
        "    return examples\n",
        "\n",
        "# Create a dataframe of topic/subtopic/datasource/idea\n",
        "data = []\n",
        "for subtopic, ds in datasources.items():\n",
        "  for d in ds:\n",
        "        story_ideas = generate_story_ideas(subtopic, d)\n",
        "        data.append({\"topic\": topic, \"subtopic\": subtopic, \"datasource\": d, \"idea\": story_ideas})\n",
        "\n",
        "df = pd.DataFrame(data)\n",
        "print(df)\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "Havir0g_UQyT"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df.to_csv(\"SteroidsIdeas.csv\")"
      ],
      "metadata": {
        "id": "nVek0wOVIgXX"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}